\section{Implementation}
\label{sec:implementation}
This section describes in more detail the design choices of the implementation
of the theory from section \ref{sec:theory}. Figure \ref{fig:tree} represents
our program. It contains every class we use. The arrows represent a reference to
the other class.

\begin{figure}
\centering
\caption{The tree, representing the structure of our program}
\xymatrix{  & \textrm{ScotlandYard} \ar@/^/[ddl] \ar[d]\\
& \textrm{Map} \ar@/_/[dr] \\
\textrm{GUI} \ar@/^/[uur] \ar@/_/[ur] & \textrm{Nodes} \ar[u] &
\textrm{Agents} \ar@/_/[ul]\\
& & \textit{Detectives} \ar[u] & \textit{Mr X} \ar[ul]\\
}
\label{fig:tree}
\end{figure}

\subsection{Map and Initialisation} % {{{
\label{sub:map}
The board is represented my a \texttt{Map} object consisting of an array of 200
nodes and the 6 agents (5 detectives and Mr. X.).
6 random positions are picked for the agents. It is possible to pass a seed
from the command-line to test the same board multiple times.

Our map is represented by two .txt files and a jpeg. The jpeg is an image of the
Scotland Yard Game board. The text files contain useful information about this
game board for the program.
\subsubsection{Data Files} % {{{
\label{ssub:files}
When the program is started, the \texttt{map.txt} file is read from the
\texttt{data} directory to parse the connections and add them to the Node
objects. \texttt{map.jpg} and \texttt{positions.txt} are used to show the board
and to depict the agents in the correct positions respectively.

\texttt{map.txt} was obtained from the open source scotland-yard program from
\texttt{http://sourceforge.net/projects/scotland-yard/}, but had several
errors, so it was necessary to check all connections by hand.

The positions of the nodes on the image file were obtained by creating a small
javascript script to capture the positions on clicking them with a mouse. The
script is in the source code at \texttt{data/capture.html}
% subsubsection files }}}
% subsection map }}}

\subsection{Node} % {{{
\label{sub:node}
A \texttt{Node} object contains an \texttt{IdentityHashTable} of connections to
other nodes, and 4 \texttt{Linkedlists} for each type of connection that refers to
neighbouring nodes. There are several different methods that return iterables
over connected nodes and methods to check wether nodes are connected.
% subsection node }}}

\subsection{Agents and Detectives} % {{{
\label{sub:agent}
The Map object has 5 Detectives and one Mr. X. These are contained in an array
of Agent objects, which is the abstract superclass of Detective and MrX.

Agents have a number of tickets of different types, and a reference to the node
of its current position. the \texttt{moveToPosition} method handles the tickets
and new position.
% subsection agent }}}

\subsection{Mr. X. and AI} % {{{
\label{sub:ai}
Mr. X. is controlled by the computer. The AI calculates the best action based
on several factors: To each node that is connected to his current position a
value is assigned that represents how safe it is.

The value is the product of the number of connections, a function of the
proximity of the closest detective\footnote{1.0 for 4 nodes away of further,
0.4 for 3 nodes away, 0.2 for 2 nodes away, 0.01 for one node away, and 0.0 for
a detective in that exact spot}, the number of transport types, and the amount
of tickets he has that can bring him to the node.

Mr. X. travels to the node with the highest assigned value, and takes the type
of transport for which he has most tickets and which connects the two nodes.

\subsection{GUI} % {{{
\label{sub:GUI}
We made a GUI for getting input for how to move the agent from the user. This
input is given by mouse clicks. The screen size is variable, so we had to be
able to move the map. This is done by using our \texttt{MyMouseListener} class,
which implements the \texttt{MouseInputAdapter}. This enabled us to distinguish
between clicking and dragging the mouse, so we could create an intuitive
interface.
Dragging the mouse moves the map imag, while clicking can select an Agent or
Node.

\subsubsection{Finding out what you want to click}
When you click the screen, an event is fired to our mouse listener. This enables
us to know where you clicked. What we do to find out which agent you wanted to
click is simply translate the point to the correct point in the map, and compare
this to the positions of all the Agents. The closest agent is interpreted as the
one you want to click.

When you have clicked an agent, the listener knows you probably want to click a
Node next. When interpreting the next click, we should not compare the position
you clicked to \textbf{all} the nodes in the map. That is why we retrieve the
node the Agent is standing on, and only check for the nodes where you are able
to click.

When you clicked a node, a dialog appears, telling you which node you clicked
and asking you how you want to go there. There is also the possibility to cancel
your click, by closing the dialog.
After doing all that, the move method in \texttt{ScotlandYard} is called with
the agent number, and which move you want to do (specified by a
\texttt{Movement}).

% subsection GUI }}}

\subsection{ScotlandYard} % {{{
\label{sub:sy}
The ScotlandYard class creates an instance of itself and invokes the Map and
GUI constructors. When the game has started its method \texttt{move} can be
invoked by the GUI whenever the user makes an action.

\subsubsection{\texttt{ScotlandYard.move()}} % {{{
\label{ssub:sy.move}
The move method controls the flow of the game. It gets the ID of the detective that
wants to move, the node it wants to move to and the method of transportation.
It keeps track of which agents have moved each turn and throws an exception if
the agent has already moved. Otherwise it will proceed to move the detective
and check if Mr. X. is captured. If all detectives have moved, Mr. X. makes a
move and it is checked whether turn 24 is over yet.
% subsubsection sy.move() }}}
% subsection sy }}}
